package com.zeng.sys.authmd.apimodule.vo;

import com.zeng.nicecore.validation.ValidGroup;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.util.DigestUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class UserVo {

    //用户code
    @NotBlank(groups = {ValidGroup.add.class,ValidGroup.update.class},message = "账号不能为空")
    private String userAccount;

    //角色标识
    private String fullName;

    //系统管理员配置
    private String userConfig;

    //用户密码
    @NotEmpty(groups = {ValidGroup.add.class},message = "密码不能为空")
    private String userPw;

    //账号是否锁定，1：锁定，0未锁定
    @NotNull(groups = {ValidGroup.add.class,ValidGroup.update.class},message = "是否锁定不能为空")
    private Short locked;

    private List<String> roles;

    private Date loginTime;

    private Date expireTime;

    private Set<String> apis;


    private Integer version;

    private Short valid;

    private String code;

    private Integer id;

    private Date updateDate;

    private Date insertDate;

    public String getMd5Pw(String salt) {
        String pwmd5 = null;
        if (this.getUserPw() == null) {
            return null;
        }
        String utf8 = DigestUtils.md5DigestAsHex(salt.getBytes(StandardCharsets.UTF_8));
        pwmd5 = DigestUtils.md5DigestAsHex((this.getUserPw() + utf8).getBytes(StandardCharsets.UTF_8));
        return pwmd5;
    }
}